/*
 * Copyright 2018 JessYan
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package me.work.pay.jsyl.app.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Parcelable;
import android.util.Log;

import com.alibaba.android.arouter.launcher.ARouter;

import java.io.Serializable;

import me.work.pay.jsyl.app.Constants;


/**
 * ================================================
 * Created by JessYan on 30/03/2018 17:16
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * ================================================
 */
public class Utils {
    private Utils() {
        throw new IllegalStateException("you can't instantiate me!");
    }
    /** 判断网络是否连接 */
    public static boolean isConnectIsNormal(Context context) {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info != null && info.isAvailable()) {
            String name = info.getTypeName();
            Log.e("======", "联网结果：--name："+name);
            return true;
        } else {
            return false;
        }
    }
    /**
     * 使用 {@link ARouter} 根据 {@code path} 跳转到对应的页面, 这个方法因为没有使用 {@link Activity}跳转
     * 所以 {@link ARouter} 会自动给 {@link Intent} 加上 Intent.FLAG_ACTIVITY_NEW_TASK
     * 如果不想自动加上这个 Flag 请使用 {@link ARouter#getInstance()#navigation(Context, String)} 并传入 {@link Activity}
     *
     * @param path {@code path}
     */
    public static void navigation(String path) {
        ARouter.getInstance().build(path).navigation();
    }

    /**
     * 使用 {@link ARouter} 根据 {@code path} 跳转到对应的页面, 如果参数 {@code context} 传入的不是 {@link Activity}
     * {@link ARouter} 就会自动给 {@link Intent} 加上 Intent.FLAG_ACTIVITY_NEW_TASK
     * 如果不想自动加上这个 Flag 请使用 {@link Activity} 作为 {@code context} 传入
     *
     * @param context
     * @param path
     */
    public static void navigation(Context context, String path) {
        ARouter.getInstance().build(path).navigation(context);
    }

    public static void navigationWithTitle(String path, String name) {
        ARouter.getInstance().build(path).withString(Intent.EXTRA_TITLE, name).navigation();
    }

    public static void navigationWithTitle(Context context, String path, String name) {
//        ARouter.getInstance().build(path).withString(Intent.EXTRA_TITLE,name).navigation(context);
        ARouter.getInstance().build(path).withString(Intent.EXTRA_TITLE, name).navigation(context);
    }

    public static void customNavigation(String path, String title, Parcelable data) {
        ARouter.getInstance().build(path).withParcelable(Constants.PARCELABLE_DATA, data).withString(Intent.EXTRA_TITLE, title).navigation();
    }
    public static void customNavigation(String path, String title, String data) {
        ARouter.getInstance().build(path).withString(Constants.ROUTER_STRING, data).withString(Intent.EXTRA_TITLE, title).navigation();
    }
    public static void customNavigation(String path, String title,int intData, String data) {
        ARouter.getInstance().build(path).withInt(Constants.ROUTER_INTEGER,intData).withString(Constants.ROUTER_STRING, data).withString(Intent.EXTRA_TITLE, title).navigation();
    }
    public static void customNavigation(String path, String title,int intData, String data,Parcelable parcelable) {
        ARouter.getInstance().build(path).withInt(Constants.ROUTER_INTEGER,intData).withParcelable(Constants.PARCELABLE_DATA,parcelable).withString(Constants.ROUTER_STRING, data).withString(Intent.EXTRA_TITLE, title).navigation();
    }

    public static void customNavigation(String path, String title, int intData, Parcelable data) {
        ARouter.getInstance().build(path).withInt(Constants.ROUTER_INTEGER, intData).withParcelable(Constants.PARCELABLE_DATA, data).withString(Intent.EXTRA_TITLE, title).navigation();
    }

    public static void customNavigation(String path, String title, boolean bool, Parcelable data) {
        ARouter.getInstance().build(path).withBoolean(Constants.ROUTER_BOOLEAN, bool).withParcelable(Constants.PARCELABLE_DATA, data).withString(Intent.EXTRA_TITLE, title).navigation();
    }
    public static void customNavigation(String path, String title, boolean bool) {
        ARouter.getInstance().build(path).withBoolean(Constants.ROUTER_BOOLEAN, bool).withString(Intent.EXTRA_TITLE, title).navigation();
    }

    public static void customNavigation(String path, String title, int intData) {
        ARouter.getInstance().build(path).withInt(Constants.ROUTER_INTEGER, intData).withString(Intent.EXTRA_TITLE, title).navigation();
    }

    public static void customNavigation(String path, String title, Serializable data) {
        ARouter.getInstance().build(path).withSerializable(Constants.SERIALIZABLE_DATA, data).withString(Intent.EXTRA_TITLE, title).navigation();
    }
}
